ActiveAdmin.register RePurchaseOverview do
  menu label: '复购率概括', parent: '数据统计'

  actions :index
  filter :company, label: '集团'
  controller do
    def index
      @company = params[:q][:company_contains] rescue ""
      index! do |format|
        format.html {render 'admin/re_purchase_overviews/index', layout: 'active_admin'}
      end
    end

    def data
      render json: {
          total: RePurchaseOverview.ransack(company_contains: params[:company]).result.count,
          rows: RePurchaseOverview.ransack(company_contains: params[:company]).result.order(company: :desc, report_date: :desc).map {|r|
            {
                report_date: r.report_date,
                company: r.company,
                purchaser: r.purchaser,
                month_1: r.month_1,
                month_2: r.month_2,
                month_3: r.month_3,
                month_4: r.month_4,
                month_5: r.month_5

            }}
      }
    end
  end
end
